﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Linq.Expressions;
namespace Zhjt.Comm.Lib.Tools.EnumTools
{
    public  class EnumTools
    {

        /// <summary>
        /// 将指定枚举类型转换为Dictionary,枚举值作为key, 文本作为value
        /// </summary>
        /// <param name="enumType">枚举类型</param>
        /// <param name="text">要作为value的文本,多个以逗号分隔</param>
        /// <param name="orderType">1 枚举值正序插入  2 枚举值倒序插入</param>
        /// <returns></returns>
        public static Dictionary<string, string> EnumConvertDictionary(Type enumType, string text = "", int orderType = 1)
        {

            Dictionary<string, string> di = new Dictionary<string, string>();
            if (string.IsNullOrWhiteSpace(text)) return di;
            string[] texts = text.Split(',');
            List<int> values = GetEnumValues(enumType);
            if (orderType == 2) values = values.OrderByDescending(c => c).ToList();
            for (int i = 0; i < values.Count; i++)
            {
                if (texts.Length > i)
                {
                    di.Add(values[i].ToString(), texts[i]);
                }
                else
                {
                    di.Add(values[i].ToString(), "");
                }
            }
            return di;
        }
       
        /// <summary>
        /// 将指定枚举类型转换为Dictionary,枚举值作为key, 文本作为value
        /// </summary>
        /// <param name="enumType">枚举类型</param>
        /// <param name="text">要作为value的文本,多个以逗号分隔</param>
        /// <param name="orderType">1 枚举值正序插入  2 枚举值倒序插入</param>
        /// <returns></returns>
        public static Dictionary<int, string> EnumConvertDictionaryInt(Type enumType, string text = "",int orderType=1)
        {
            Dictionary<int, string> di = new Dictionary<int, string>();
            if (string.IsNullOrWhiteSpace(text)) return di;
            string[] texts = text.Split(',');
            List<int> values = GetEnumValues(enumType);
            if (orderType == 2) values = values.OrderByDescending(c => c).ToList();
            for (int i = 0; i < values.Count; i++)
            {
                if (texts.Length > i)
                {
                    di.Add(values[i], texts[i]);
                }
                else
                {
                    di.Add(values[i], "");
                }
            }
            return di;
        }
        /// <summary>
        /// 获取枚举类型的所有值,返回泛型集合
        /// </summary>
        /// <param name="enumType">枚举类型</param>
        /// <returns></returns>
        public static List<int> GetEnumValues(Type enumType)
        {
            var values = Enum.GetValues(enumType);
            List<int> result = new List<int>();
            foreach (var v in values)
            {
                result.Add((int)v);
            }
            return result;
        }
    }
}
